import { Component, OnInit } from "@angular/core";
import { Router, ActivatedRoute, Params }   from '@angular/router';
import { Observable } from 'rxjs/Observable';

import { scroll } from "../f10-shared/common-methods";
import { TableTab, TableHeader } from "../f10-shared/interface-shared";
import { hasownproperty, removeHMS } from "../f10-shared/util";
import { F10Service } from "../service/f10.service";
import { ScrollService } from "../service/scroll.service"

@Component({
  selector: 'comp-profile',
  templateUrl: 'comp-profile.component.html',
  styleUrls: ['comp-profile.component.less']
})
export class CompProfileComponent implements OnInit {
  timer = null;
  tabClicked = {
    flag: false
  };
  activeTabIndex = 0;
  scrollTimer = null;
  mustChange = false;
  subLists: TableTab[] = [
    {
      label: "基本情况",
      class: 'sub-title-0'
    },
    {
      label: "发行相关",
      class: 'sub-title-1'
    },
    // {
    //   label: "投资关系图历程",
    //   class: 'sub-title-2'
    // },
    // {
    //   label: "发展历程",
    //   class: 'sub-title-3'
    // }
  ]
  symbol: string = ''
  basicHeader: TableHeader[] = [
    {
      label: "公司名称：",
      key: "chiName"
    },
    {
      label: "公司英文名称：",
      key: "engName",
    },
    {
      label: "网址：",
      key: "website"
    },
    {
      label: "证券简称：",
      key: "secuAbbr"
    },
    {
      label: "证券代码：",
      key: "secuCode"
    },
    {
      label: "所属行业：",
      key: "firstIndustryName"
    },
    {
      label: "所属地区：",
      key: "state"
    },
    {
      label: "注册地址：",
      key: "regAddr"
    },
    {
      label: "法人代表：",
      key: "legalRepr"
    },
    {
      label: "总经理：",
      key: "generalManager"
    },
    {
      label: "董事会秘书：",
      key: "secretaryBD"
    },
    {
      label: "证券事务代表：",
      key: "secuAffairsRepr"
    },
    {
      label: "联系电话：",
      key: "contactTel"
    },
    {
      label: "电子邮箱：",
      key: "contactEmail"
    },
    {
      label: "主营业务：",
      key: "businessMajor"
    },
    {
      label: "公司介绍：",
      key: "briefIntroText"
    }
  ]
  releasesHeader: TableHeader[] = [
    {
      label: "上市日期",
      key: "listDate"
    },
    {
      label: "发行公告日",
      key: "issuePublDate"
    },
    {
      label: "发行价",
      key: "issuePrice"
    },
    {
      format: true,
      label: "发行数量(股)",
      key: "issueVol"
    },
    {
      format: true,
      label: "募集资金总额(元)",
      key: "IPOProceeds"
    },
    {
      format: true,
      label: "募集资金净额(元)",
      key: "IPONetProceeds"
    },
    {
      label: "主承销商",
      key: "leadUnderwriter"
    },
    {
      format: true,
      label: "其中老股转让发行量(股)",
      key: "OriginalVol"
    },
    {
      format: true,
      label: "首发后总股本_上市日(股)",
      key: "totalSharesListDate"
    },
    {
      format: true,
      label: "首发前总股本(股)",
      key: "totalSharesBeforeIssue"
    },
    {
      format: true,
      label: "发行总市值(元)",
      key: "totalIssueMV"
    },
    {
      format: true,
      label: "上市首日开盘价(元)",
      key: "firstOpenPrice"
    },
    {
      format: true,
      label: "上市首日最高价(元)",
      key: "firstHighPrice"
    },
    {
      format: true,
      label: "上市首日最低价(元)",
      key: "firstLowPrice"
    },
    {
      format: true,
      label: "上市首日收盘价(元)",
      key: "firstClosePrice"
    },
    {
      format: true,
      label: "上市首日成交均价(元)",
      key: "firstAvgPrice"
    },
    {
      format: true,
      label: "上市首日成交量(股)",
      key: "firstTurnoverVolume"
    },
    {
      format: true,
      label: "上市首日成交额(元)",
      key: "firstTurnoverValue"
    },
    {
      format: true,
      label: "上市首日涨跌幅(%)",
      key: "firstChangePCT"
    },
    {
      format: true,
      label: "上市首日换手率(%)",
      key: "firstTurnover"
    }
  ]
  compInfoSource: Observable<any>;
  ipoinfoSource: Observable<any>;
  constructor (
    private f10Service: F10Service,
    private router: ActivatedRoute,
    private scrollService: ScrollService
  ) {}
  ngOnInit () {
    let routerParamsStream = this.router.parent.parent.params.map(params => params["id"]).filter(id => !!id).map(id => {
      let stock = {
        symbol: (<string>id).replace("_", ".")
      };
      this.symbol = stock.symbol
      return stock
    })
    // 公司概况
    this.compInfoSource = routerParamsStream.switchMap((stock) => this.f10Service.getCompanyinfo('companyinfo', stock.symbol))
    // 发行相关信息
    this.ipoinfoSource = routerParamsStream.switchMap((stock) => this.f10Service.getCompanyinfo('ipoinfo', stock.symbol))
      .map((data: any) => ({
          ...data,
          issuePublDate: removeHMS(data.issuePublDate),
          listDate: removeHMS(data.listDate)
        })
      )
  }

  tabToggle (event) {
    this.tabClicked.flag = true
    this.timer = scroll(`segment-container`, event, this.timer, this.tabClicked);
  }

  scrollEvent (e, el, el1, el2, el3, el4, el5) {
    if (this.scrollTimer) clearTimeout(this.scrollTimer)
    this.scrollTimer = setTimeout(() => {
      this.mustChange = !this.mustChange
      this.tabClicked.flag = false
      this.activeTabIndex = this.scrollService.tabIndex(e, el, el1, el2, el3, el4, el5)
    }, 50)
    if (this.tabClicked.flag) return
    this.activeTabIndex = this.scrollService.tabIndex(e, el, el1, el2, el3, el4, el5)
  }
}